package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.BMessageKefu;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.BMessageKefuService;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 微信公众号客服联系方式(BMessageKefu)表控制层
 *
 * @author makejava
 * @since 2020-12-22 09:54:14
 */
@RestController
@RequestMapping("bMessageKefu")
public class BMessageKefuHandler {
    /**
     * 服务对象
     */
    @Resource
    private BMessageKefuService bMessageKefuService;

    /**
     * 查询多条数据
     *
     * @return 实例对象
     */
    @GetMapping(value = "/selectAllByLimit", produces = "application/json;charset=UTF-8")
    public RespData selectAll(@RequestHeader("userId") String userId, @RequestHeader("identifying") String springProfilesActive) {
        RespData respData = new RespData();

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
        String keFuQrCode = "";

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(springProfilesActive)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        BMessageKefu bMessageKefu = new BMessageKefu();
        bMessageKefu.setGzhCode(springProfilesActive);

        List<BMessageKefu> queryAllByLimitDto = bMessageKefuService.queryAll(bMessageKefu);
        if (CollectionUtils.isNotEmpty(queryAllByLimitDto)) {

            keFuQrCode = queryAllByLimitDto.get(new Random().nextInt(queryAllByLimitDto.size())).getPicUrl();

            List<BMessageKefu> collect = queryAllByLimitDto.stream().map(info -> {

                BMessageKefu temporary = new BMessageKefu();
                temporary.setPicUrl(info.getPicUrl());

                return temporary;
            }).collect(Collectors.toList());

            resultMap.put("collect", collect);

        }

        resultMap.put("keFuQrCode", keFuQrCode);

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

}